package com.rocket.consumer.controller;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RocketMQMessageListener(topic = "user1", consumerGroup = "my-consumer-group")
public class UserController implements RocketMQListener<String> {
    String key = "user2";

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String str) {
        System.out.println("User接收信息：" + str);
        if (null != str && !"".equals(str)){
            redisTemplate.opsForValue().set(key, str);
        }
    }
    @GetMapping("/getUser")
    public String getUser(){
        String str = (String)redisTemplate.opsForValue().get(key);
        if (null != str && !"".equals(str)){
            return "用户信息为:" + str;
        }
        return "用户还没注册";
    }
}
